package com.gexin.common.kusy;

import com.gexin.common.utils.HttpKit;
import com.gexin.common.utils.StringUtil;
import com.jfinal.kit.JsonKit;

import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class KusyApi {

    /**
     * 微信公众号下单
     * @param appid         公众号appid
     * @param openid        公众号用户openid
     * @param orderid       订单id
     * @param money         金额
     * @param remarks       备注
     * @param notify_url    回调页面
     * @return
     */
    public static String pub_wxpay(String apikey,String signkey,String appid,String openid,String orderid,double money,String remarks,String notify_url){
        String url = KusyUtil.Api_Url+"/payapi/mini/wxpay";
        String service = "comm.js.pay";
        return wxpay(apikey,signkey,url,service,appid,openid,orderid,money,remarks,notify_url);
    }

    /**
     * 微信公众号下单（分账订单）
     * @param appid         公众号appid
     * @param openid        公众号用户openid
     * @param orderid       订单id
     * @param money         金额
     * @param remarks       备注
     * @param notify_url    回调页面
     * @return
     */
    public static String pub_wxpay_share(String apikey,String signkey,String appid,String openid,String orderid,double money,String remarks,String notify_url){
        String url = KusyUtil.Api_Url+"/payapi/mini/wxpay";
        String service = "wxpay.js.pay";
        return wxpay(apikey,signkey,url,service,appid,openid,orderid,money,remarks,notify_url);
    }

    /**
     * 微信小程序下单
     * @param appid         小程序appid
     * @param openid        小程序用户openid
     * @param orderid       订单id
     * @param money         金额
     * @param remarks       备注
     * @param notify_url    回调页面
     * @return
     */
    public static String mini_wxpay(String apikey,String signkey,String appid,String openid,String orderid,double money,String remarks,String notify_url){
        String url = KusyUtil.Api_Url+"/payapi/mini/wxpay";
        String service = "comm.mini.pay";
        return wxpay(apikey,signkey,url,service,appid,openid,orderid,money,remarks,notify_url);
    }

    /**
     * 微信小程序下单(分账订单)
     * @param appid         小程序appid
     * @param openid        小程序用户openid
     * @param orderid       订单id
     * @param money         金额
     * @param remarks       备注
     * @param notify_url    回调页面
     * @return
     */
    public static String mini_wxpay_share(String apikey,String signkey,String appid,String openid,String orderid,double money,String remarks,String notify_url){
        String url = KusyUtil.Api_Url+"/payapi/mini/wxpay";
        String service = "wxpay.mini.pay";
        return wxpay(apikey,signkey,url,service,appid,openid,orderid,money,remarks,notify_url);
    }


    /**
     * 黎峻  APP支付
     * @param apikey
     * @param signkey
     * @param orderid
     * @param money
     * @param remarks
     * @param notify_url
     * @return
     */
    public static  String mini_wxpay_app(String apikey,String signkey,String orderid,double money,String remarks,String notify_url){
        String url = KusyUtil.Api_Url+"/payapi/mini/app";
        String service = "pay.wxpay.app";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service",service);
        params.put("apikey", apikey);
        params.put("money",String.valueOf(money));
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("sub_appid","wx794f110f8412df72");
        params.put("remarks",remarks);
        params.put("mch_orderid",orderid);
        params.put("notify_url",notify_url);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    public static String wxpay(String apikey,String signkey,String url,String service,String appid,String openid,String orderid,double money,String remarks,String notify_url){
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service",service);
        params.put("apikey", apikey);
        params.put("money",String.valueOf(money));
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("sub_appid",appid);
        params.put("sub_openid",openid);
        params.put("remarks",remarks);
        params.put("mch_orderid",orderid);
        params.put("notify_url",notify_url);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 退款
     * @param orderid           系统订单号
     * @param money             退款金额
     * @param mch_refund_no     商户退款订单号，多次退款时需传，如果多次退款传一样的值话，只会对上一次退款结果进行查询返回。
     * @return
     */
    public static String refund(String apikey,String signkey,String orderid,double money,String mch_refund_no){
        String url = KusyUtil.Api_Url+"/payapi/pay/refund_order";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.comm.refund_order");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("orderid",orderid);
        params.put("refundmoney",String.valueOf(money));
        if (StringUtil.isNotBlank(mch_refund_no)){
            params.put("mch_refund_no",mch_refund_no);
        }
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    public static String refund_lijun(String apikey,String signkey,String mch_orderid,double money,String mch_refund_no){
        String url = KusyUtil.Api_Url+"/payapi/pay/refund_order";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.comm.refund_order");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("mch_orderid",mch_orderid);
        params.put("refundmoney",String.valueOf(money));
        if (StringUtil.isNotBlank(mch_refund_no)){
            params.put("mch_refund_no",mch_refund_no);
        }
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 退款查询
     * @param orderid           系统订单号
     * @param money             退款金额
     * @param mch_refund_no     商户退款订单号，多次退款时需传，如果多次退款传一样的值话，只会对上一次退款结果进行查询返回。
     * @return
     */
    public static String refund_query(String apikey,String signkey,String orderid,double money,String mch_refund_no){
        String url = KusyUtil.Api_Url+"/payapi/pay/query_refund";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.comm.query_refund");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("orderid",orderid);
        if (StringUtil.isNotBlank(mch_refund_no)){
            params.put("mch_refund_no",mch_refund_no);
        }
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 是否开启分账功能
     * @param is_share          1开启，0关闭
     * @param share_proportion  这个apikey所属门店可分配资金最大比例，不能大于微信后台分账模式配置的可分账比例 单位%，10的整数倍，10表示10%
     * @return
     */
    public static String share_on(String apikey,String signkey,String is_share,Integer share_proportion){
        String url = KusyUtil.Api_Url+"/payapi/pay/receiver_config";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.share_on");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("is_share",is_share);
        if (share_proportion!=null){
            params.put("share_proportion",share_proportion.toString());
        }
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 添加分账接收方
     * @param type      分账账号类型 MERCHANT_ID时，是商户ID ； 类型是PERSONAL_WECHATID时，是个人微信号
     * @param account   分账接收方帐号；type类型是MERCHANT_ID时，是商户ID 类型是PERSONAL_WECHATID时，是个人微信号
     * @param name      分账接收方类型是MERCHANT_ID时，是商户全称， 分账接收方类型是PERSONAL_NAME 时，是个人姓名
     * @return
     */
    public static String add_receiver(String apikey,String signkey,String type,String account,String name){
        String url = KusyUtil.Api_Url+"/payapi/pay/receiver_config";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.add_receiver");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("type",type);
        params.put("account",account);
        params.put("name",name);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 删除分账接收方
     * @param type      分账账号类型 MERCHANT_ID时，是商户ID ； 类型是PERSONAL_WECHATID时，是个人微信号
     * @param account   分账接收方帐号；type类型是MERCHANT_ID时，是商户ID 类型是PERSONAL_WECHATID时，是个人微信号
     * @param name      分账接收方类型是MERCHANT_ID时，是商户全称， 分账接收方类型是PERSONAL_NAME 时，是个人姓名
     * @return
     */
    public static String del_receiver(String apikey,String signkey,String type,String account,String name){
        String url = KusyUtil.Api_Url+"/payapi/pay/receiver_config";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.del_receiver");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("type",type);
        params.put("account",account);
        params.put("name",name);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 分账回退
     * @param orderid
     * @param money
     * @param account
     * @return
     */
    public static String sharing_return(String apikey,String signkey,String orderid,double money,String account){
        String url = KusyUtil.Api_Url+"/payapi/pay/sharing_return";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.sharing_return");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("money",String.valueOf(money));
        params.put("orderid",orderid);
        params.put("type", KusyUtil.AccountType.MERCHANT_ID.toString());
        params.put("account",account);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 分账回退查询
     * @param orderid
     * @param money
     * @param account
     * @return
     */
    public static String sharing_return_query(String apikey,String signkey,String orderid,String sharing_return_no){
        String url = KusyUtil.Api_Url+"/payapi/pay/sharing_return_query";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.sharing_return_query");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("orderid",orderid);
        params.put("sharing_return_no", sharing_return_no);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    public static String order_sharing(String apikey,String signkey,double money, String orderid, List<KusyOrderShareReceiver> receivers){
        return order_sharing(apikey,signkey,money,orderid,null,null, JsonKit.toJson(receivers));
    }

    /**
     * 订单分账
     * @param apikey
     * @param signkey
     * @param money             分账金额
     * @param orderid           支付系统订单号
     * @param mch_orderid       商户订单号
     * @param order_time        下单时间
     * @param share             分账接收方json
     * @return
     */
    public static String order_sharing(String apikey,String signkey,double money,String orderid,String mch_orderid,String order_time,String share){
        System.out.println(share);
        String url = KusyUtil.Api_Url+"/payapi/pay/order_sharing";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.order_sharing");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("money",String.valueOf(money));
        params.put("orderid",orderid);
        if (StringUtil.isNotBlank(mch_orderid)){
            params.put("mch_orderid",mch_orderid);
            params.put("order_time",order_time);
        }
        params.put("sharing_detail",share);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 查询分账结果
     * @param apikey
     * @param signkey
     * @param orderid       支付系统订单id
     * @return
     */
    public static String query_sharing(String apikey,String signkey,String orderid){
        String url = KusyUtil.Api_Url+"/payapi/pay/query_sharing";
        SortedMap<String,String> params = new TreeMap<>();
        params.put("service","pay.wxpay.query_sharing");
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("orderid",orderid);
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }

    /**
     * 扫码支付
     * @param apikey
     * @param signkey
     * @param money         金额
     * @param type          类型
     * @param order_id      商户订单号
     * @param notify_url    回调地址
     * @return
     */
    public static String scan_pay(String apikey,String signkey,String order_id,double money,String type,String notify_url){
        String url = KusyUtil.Api_Url+"/payapi/pay/qrcode";
        SortedMap<String,String> params = new TreeMap<>();
        if (type.equals("wxpay")){
            params.put("service","wxpay.comm.qrcode");
        }else if(type.equals("alipay")){
            params.put("service","alipay.comm.qrcode");
        }
        params.put("apikey", apikey);
        params.put("nonce_str", KusyUtil.Nonce_Str);
        params.put("money",String.valueOf(money));
        params.put("mch_orderid",order_id);
        params.put("notify_url",notify_url);
        params.put("remarks","商户账号充值");
        String sign = KusyUtil.createSign(params, signkey);
        params.put("sign",sign);
        String result = HttpKit.post(url,params);
        System.out.println(result);
        return result;
    }
}
